<script setup>
import { ref,reactive,onBeforeMount } from 'vue'
import useAdminStore from '../../store/adminStore'
import Dialog from './dialog.vue'

const adminStore = useAdminStore()

// Dialog组件上的ref
const dialog = ref(null)

onBeforeMount(() => {
  // 获取管理员数据
  adminStore.getAdminsAction()
  // 获取总数
  adminStore.getAdminsTotalAction()
})

// 修改页码
function changePage(page) {
  adminStore.changePage(page)
  adminStore.getAdminsAction()
}
// 修改条数
function changeSize(size) {
  adminStore.changeSize(size)
  adminStore.getAdminsAction()
}

// 新增
function addAdmin() {
  // 打开对话框
  adminStore.openDialog()
}
// 编辑
function editAdmin(admin) {
  // console.log(admin,'编辑')
  dialog.value.editId = admin.uid
  dialog.value.usersForm.username = admin.username
  dialog.value.usersForm.password = admin.password
  dialog.value.usersForm.roleid = admin.roleid
  dialog.value.usersForm.status = admin.status
  // 打开对话框
  adminStore.openDialog()
}
// 删除
function confirmEvent(id) {
  // console.log(id,'删除')
  adminStore.delAdminAction(id)
}
</script>

<template>
  <el-card>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统设置</el-breadcrumb-item>
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="right">
      <el-button @click="addAdmin" type="primary">新增管理员</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="adminStore.adminData.list" style="width: 100%" max-height="400">
      <el-table-column label="序号" fixed width="80" prop="id"></el-table-column>
      <el-table-column label="用户账号" width="180" prop="username"></el-table-column>
      <el-table-column label="用户权限" width="180">
        <template v-slot="props">
          <el-tag size="small">{{ props.row.roleid }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="180">
        <template v-slot="props">
          <el-tag type="success" size="small" v-if="props.row.status === 1">启用</el-tag>
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" min-width="150">
        <template v-slot="props">
          <el-button type="primary" size="small" round @click="editAdmin(props.row)">编辑</el-button>
          <!-- 气泡确认框 -->
          <el-popconfirm
            width="160px"
            title="你确定要删除吗？"
            @confirm="confirmEvent(props.row.uid)"
          >
            <template #reference>
              <el-button type="danger" size="small" round>删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="adminStore.total"
      :current-page="adminStore.page"
      :page-size="adminStore.size"
      :page-sizes="[1, 5, 10, 20, 50, 100]"
      @size-change="changeSize"
      @current-change="changePage"
    />
  </el-card>
  <!-- 对话框 -->
  <Dialog ref="dialog"></Dialog>
</template>

<style lang="scss" scoped>
.el-pagination {
  margin: 20px auto;
}
</style>
